﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using HCMServices.DTO;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;

namespace HCMServices.DAO
{
    /// <summary>
    /// Class to process for QUANHUYEN Table 
    /// </summary>
    public class DistrictDAO
    {
        private MyDataContext db;

        /// <summary>
        /// Constructor of class
        /// </summary>
        /// <param name="connectString">String indentify how to connect database</param>
        public DistrictDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        /// <summary>
        /// This function gets all of district in database
        /// </summary>
        /// <returns>List of District</returns>
        public List<DistrictDTO> GetAllQuanHuyenDTO()
        {
            List<DistrictDTO> lstQuanHuyenDTO = new List<DistrictDTO>();

            IList<QUANHUYEN> districtQuery = (from QUANHUYEN district in db.District
                                      select district).ToList();

            foreach (QUANHUYEN d in districtQuery)
            {
                DistrictDTO temp = new DistrictDTO();
                temp.Id = d.MaQuanHuyen;
                temp.Name = d.TenQuanHuyen;
                lstQuanHuyenDTO.Add(temp);
            }

            return lstQuanHuyenDTO;
        }

        /// <summary>
        /// This function find District with ID is available 
        /// </summary>
        /// <param name="id">Id contain value of District ID</param>
        /// <returns>District if id was found or null</returns>
        public DistrictDTO Search(int id)
        {
            DistrictDTO quanHuyenDTO = new DistrictDTO();

            var searchQuery = db.District
                .Where(c => (id == c.MaQuanHuyen))
                .Select(c => c);

            foreach (QUANHUYEN district in searchQuery)
            {
                quanHuyenDTO.Id = district.MaQuanHuyen;
                quanHuyenDTO.Name = district.TenQuanHuyen;
                return quanHuyenDTO;
            }

            return null;
        }

        /// <summary>
        /// This function to get ID of district
        /// </summary>
        /// <param name="name">Name of district want to get ID</param>
        /// <returns>ID of district that have Name match with name param</returns>
        public int GetIdByName(string name)
        {
            int maQuanHuyen = -1;

            var getIdQuery = db.District
                .Where(c => (name == c.TenQuanHuyen))
                .Select(c => c.MaQuanHuyen);

            foreach (int id in getIdQuery)
            {
                maQuanHuyen = id;
                return maQuanHuyen;
            }
    
            return maQuanHuyen;
        }
    }
}
